//
//  AppDelegate.m
//  Auditory
//
//  Created by 寒竹子 on 15/4/28.
//  Copyright (c) 2015年 寒竹子. All rights reserved.
//

#import "AppDelegate.h"
#import "RootNavigationController.h"
#import "MainViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <Foundation/Foundation.h>
#import "Globle.h"
#import <MediaToolbox/MediaToolbox.h>
#import "XRTabBarController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    // 设置App支持后台播放音频
    AVAudioSession * audioSession = [AVAudioSession sharedInstance];
    [audioSession setActive:YES error:nil];
    [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
    
    // 让App支持后台控制事件
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    
    [Globle sharedGloble].isPlaying = YES;
    
//    MainViewController * mainVc = [[MainViewController alloc] init];
//    RootNavigationController * rootNav = [[RootNavigationController alloc] initWithRootViewController:mainVc];
//    self.window.rootViewController = rootNav;
    XRTabBarController * tabVc = [[XRTabBarController alloc] init];
    self.window.rootViewController = tabVc;
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    
}

/**
 * @brief  应用程序进入后台
 *
 * @param
 *
 * @return
 */
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
    
    
    [Globle sharedGloble].isApplicationEnterBackground = YES;
    
    // 发送进入后台的通知
    [[NSNotificationCenter defaultCenter] postNotificationName:XRRadioViewSetSongInfoNotifiCation object:nil userInfo:nil];
}

/**
 * @brief  锁屏后台控制
 *
 * @param
 *
 * @return
 */
- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
    NSString * statu = nil;
    if (event.type == UIEventTypeRemoteControl) {
        // 控制事件
        switch (event.subtype) {
            case UIEventSubtypeRemoteControlPause: // 暂停
                statu = @"UIEventSubtypeRemoteControlPause";
                break;
            case UIEventSubtypeRemoteControlPreviousTrack: // 上一首
                statu = @"UIEventSubtypeRemoteControlPreviousTrack";
                break;
            case UIEventSubtypeRemoteControlNextTrack: // 下一首
                statu = @"UIEventSubtypeRemoteControlNextTrack";
                break;
            case UIEventSubtypeRemoteControlPlay: // 播放
                statu = @"UIEventSubtypeRemoteControlPlay";
                break;
            default:
                break;
        }
    }
    
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];
    [dict setObject:statu forKey:@"keyStatus"];
    // 发送播放状态通知
    [[NSNotificationCenter defaultCenter] postNotificationName:XRRadioViewStatusNotifiCation object:nil userInfo:dict];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    
}

/**
 * @brief  进入前台
 *
 * @param
 *
 * @return
 */
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [Globle sharedGloble].isApplicationEnterBackground = NO;
}

- (void)applicationWillTerminate:(UIApplication *)application {
    
}

@end